package com.tj.jinzao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseListAdapter;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.model.Comment;
import com.tj.jinzao.model.User;
import com.tj.jinzao.widgets.RoundedImageView;

import java.text.SimpleDateFormat;
import java.util.concurrent.CopyOnWriteArrayList;


/**
 * 日记，话题，视频评论 Adapter
 * Created by wjy on 2018/10/12.
 */
public class ArticleDetailListItemAdapter extends BaseListAdapter<Comment, ArticleDetailListItemAdapter.ViewHolder> {

    private SimpleDateFormat mSimpleDateFormat;

    public ArticleDetailListItemAdapter(Context context, CopyOnWriteArrayList data) {
        super(context, data);
        mSimpleDateFormat = new SimpleDateFormat("MM月dd日");
    }
    OnHeadCLickLinstener onHeadCLickLinstener;
    public interface OnHeadCLickLinstener{
        void onHeadClick(User user);
    }
    public void setOnHeadCLickLinstener(OnHeadCLickLinstener onHeadCLickLinstener){
        this.onHeadCLickLinstener=onHeadCLickLinstener;
    }

    private View.OnClickListener mListener;

    public void setListener(View.OnClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_comment_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Comment comment = mData.get(position);
        if (comment.getCommentId() != 0) {
            holder.comment_layout.setVisibility(View.GONE);
            holder.reply_layout.setVisibility(View.VISIBLE);
            if(comment.getUser()!=null){
                holder.replay_name.setText(comment.getUser().getNickname() + "：");
            }else {
                holder.replay_name.setText("");
            }
            holder.reply_content.setText(comment.getContent());
        } else {
            holder.comment_layout.setVisibility(View.VISIBLE);
            holder.comment_layout.setTag(R.id.data, comment.getId());
            holder.reply_layout.setVisibility(View.GONE);
            if(comment.getUser()!=null){
                holder.name.setText(comment.getUser().getNickname());
            }else {
                holder.name.setText("");
            }
            holder.content.setText(comment.getContent());
            holder.time.setText(comment.getCommentTime());
            if(comment.getUser()!=null){
                Glide.with(mContext).load(Constant.IMAGE_PREFIX + comment.getUser().getImgurl()).into(holder.head);
            }else {
                holder.head.setImageResource(R.mipmap.default_head_man);
            }
            holder.head.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(onHeadCLickLinstener!=null){
                        onHeadCLickLinstener.onHeadClick(comment.getUser());
                    }

                }
            });
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name, content, time, replay_name, reply_content;
        RoundedImageView head;
        View reply_layout, comment_layout;

        public ViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.tv_name);
            content = itemView.findViewById(R.id.tv_content);
            replay_name = itemView.findViewById(R.id.tv_reply_name);
            reply_content = itemView.findViewById(R.id.tv_reply_content);
            time = itemView.findViewById(R.id.tv_date);
            head = itemView.findViewById(R.id.iv_head);
            reply_layout = itemView.findViewById(R.id.rl_reply_layout);
            comment_layout = itemView.findViewById(R.id.rl_comment_layout);
            comment_layout.setOnClickListener(mListener);
        }
    }
}
